<?php
namespace Count\Controller;

use Think\Controller;
class ApiController extends Controller {
	
    //课程
    public function getclass(){
    	$did = I('did');
    	$class = D('YgclClass')->where(array('did'=>$did,'status'=>1))->order('listorder desc')->field('id,title')->select();
    	if($class){
    		$data = array('status'=>1,'info'=>$class);
    	}else{
    		$data = array('status'=>0,'info'=>'');
    	}
    	$this->ajaxReturn($data);
    }
    
    //班级
    public function getlesson(){
    	$cid = I('cid');
    	$class = D('YgclLesson')->where(array('cid'=>$cid,'status'=>1))->order('listorder desc')->field('id,lessonname,lessoncode,lessontime')->select();
    	if($class){
    		$data = array('status'=>1,'info'=>$class);
    	}else{
    		$data = array('status'=>0,'info'=>'');
    	}
    	$this->ajaxReturn($data);
    }
    
    public function getxiaoqu($did){
    	if($did){
    		return D('YgclDistrict')->where(array('status'=>1,'id'=>$did))->getField('title');
    	}
    }
    
    //导出统计人员
    public function export(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('学生姓名','电话','家长姓名','学期','校区','课程','班级','金额','订单号')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);

    	if(I('tid')){
    		$map['tid'] = I('tid');
    	}
		if(I('did')){
			$map['did']=I('did');
		}
		if(I('cid')){
			$map['cid']=I('cid');
		}
		if(I('lid')){
			$map['lid']=I('lid');
		}
		$map['status'] = 1;
    	$info = D('LessonStudentRelation')->where($map)->relation(true)->order('id desc')->select();

    	foreach ($info as $key => $if){
    		$data[$key] = array(
    				'0' => $if['student'],
    				'1' => $if['tel'].' ',
    				'2' => $if['jzxm'],
    				'3' => $if['term']['title'],
    				'4' => $if['xiaoqu']['title'],
    				'5' => $if['kecheng']['title'],
    				'6' => $if['banji']['lessonname'],
    				'7' => $if['order']['total'],
    				'8' => $if['order']['wxddh'].' '
    		);
    	}
    
		$excel->export($data);
		//日志
		$uid = session('loginUser.uid');
		$info = '导出学员统计信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
    //导出新增人员
    public function export_add(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('学生姓名','电话','家长姓名','学期','校区','课程','班级')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);
    
    	if(I('did')){
    		$map['did']=I('did');
    	}
    	I('start_time')?$start_time=I('start_time'):$start_time=0;
    	I('end_time')?$end_time=I('end_time'):$end_time=Date('Y-m-d');
    	
    	$map['update_time'] = array('between',array($start_time,$end_time));
    	
    	$map['status'] = 1;
    	$info = D('LessonStudentRelation')->where($map)->relation(true)->order('id desc')->select();
    	foreach ($info as $key => $if){
    		$data[$key] = array(
    				'0' => $if['student'],
    				'1' => $if['tel'].' ',
    				'2' => $if['jzxm'],
    				'3' => $if['term']['title'],
    				'4' => $if['xiaoqu']['title'],
    				'5' => $if['kecheng']['title'],
    				'6' => $if['banji']['lessonname']
    		);
    	}
    
		$excel->export($data);
		
		//日志
		$uid = session('loginUser.uid');
		$info = '导出新增人员信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
    //导出新生新增人员
    public function export_newstudent(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('学生姓名','电话','家长姓名','学期','校区','课程','班级')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);

    	if(I('did')){
    		$map['did']=I('did');
    	}else{
    		$g_did = $this->getxiaoqurole();
    		if($g_did){
    			$map['did'] = array('in',$g_did);
    		}
    	}
    	I('start_time')?$start_time=I('start_time'):$start_time=0;
    	I('end_time')?$end_time=I('end_time'):$end_time=Date('Y-m-d');
    	 
    	$map['update_time'] = array('between',array($start_time,$end_time));
    	 
    	$map['status'] = 1;
    	$map['student_status'] = array('in',array('xinsheng'));
    	$info = D('LessonStudentRelation')->where($map)->relation(true)->order('id desc')->select();
    	foreach ($info as $key => $if){
    		$data[$key] = array(
    				'0' => $if['student'],
    				'1' => $if['tel'].' ',
    				'2' => $if['jzxm'],
    				'3' => $if['term']['title'],
    				'4' => $if['xiaoqu']['title'],
    				'5' => $if['kecheng']['title'],
    				'6' => $if['banji']['lessonname']
    		);
    	}
    
		$excel->export($data);
		
		//日志
		$uid = session('loginUser.uid');
		$info = '导出新生增加人员信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
    public function getxiaoqurole(){
    	if(strpos($_SESSION['curUserRole']['title'],'XIAOQU_ADMIN')  || strpos($_SESSION['curUserRole']['title'],'XIAOWU_ADMIN')){
    		$data =  $_SESSION['curUserRole']['xiaoqu'];
    	}else{
    		$data = false;
    	}
    	
    	return $data;
    }
    
    //导出考级报名
    public function export_kjbm(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('用户','校区','备注','金额','订单号','时间')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);
    
    	$xiaoqu = R('Apiadmin/getxiaoqurole');
		if($xiaoqu){
			$map['did'] = array('in',$xiaoqu);
		}
		$map['type'] = 'kjbm';
		if(I('status')){
			$map['status'] = I('status');
		}
    	$time1 = I('start_time');
    	$time2 = I('end_time');
    	$map['update_time'] = array('between',array($time1,$time2));
    	$info = D('YgclOrder')->where($map)->relation(true)->order('id desc')->select();
    	foreach ($info as $key => $if){
    		$data[$key] = array(
    				'0' => $if['user']['realname'],
    				'4' => $if['xiaoqu']['title'],
    				'5' => $if['objectinfo'],
    				'6' => $if['total'],
    				'7' => $if['wxddh'].' ',
    				'8' => $if['create_time']
    		);
    	}
    
		$excel->export($data);
		
		//日志
		$uid = session('loginUser.uid');
		$info = '导出考级报名信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
}